/**
 * 
 */
package com.hanhai.zrb.api.facade.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.hanhai.zrb.api.biz.service.MessageQueryService;
import com.hanhai.zrb.api.biz.service.MessageSendService;
import com.hanhai.zrb.api.client.facade.MessageFacade;
import com.hanhai.zrb.api.client.result.MessageResult;
import com.hanhai.zrb.api.util.PageListUtil;
import com.hanhai.zrb.model.common.PageList;
import com.hanhai.zrb.model.message.MessageReceive;
import com.hanhai.zrb.model.message.enums.MessageBizType;

/**
 * @author leiruiqi@hanhai.com
 *
 */
@WebService(endpointInterface = "com.hanhai.zrb.api.client.facade.MessageFacade", serviceName = "MessageFacade")
@Service("messageFacade")
public class MessageFacadeImpl implements MessageFacade {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private MessageSendService messageSendService;
	
	@Resource
	private MessageQueryService messageQueryService;
	
	@Override
	public MessageResult createNotifyMessage(List<Long> receiverUids,
			String content, MessageBizType bizType, String bizKey) {

		MessageResult result = new MessageResult();
		try {
			boolean bool = this.messageSendService.createNotifyMessage(
					receiverUids, content, bizType, bizKey);
			result.setSuccess(bool);
		} catch (Exception e) {
			logger.error("do messageQueryService.createNotifyMessage exception", e);
			result.setMsg(e.getMessage());
		}
		return result;
	}

	@Override
	public MessageResult queryReceiveMessageByMaxId(Long receiverUid, Integer read,Long maxId,Integer limit) {
		MessageResult result = new MessageResult();
		try {
			List<MessageReceive> list = this.messageQueryService
					.queryReceiveMessageByMaxId(receiverUid, read, maxId, limit);
			result.setReceiveList(list);
			result.setSuccess(true);
		} catch (Exception e) {
			logger.error("do messageQueryService.queryReceiveMessageByMaxId exception", e);
			result.setMsg(e.getMessage());
		}
		return result;
	}

	@Override
	public MessageResult queryPageReceiveMessage(Long receiverUid, Integer read,int pageNum,int pageSize) {
		MessageResult result = new MessageResult();
		try {
			PageInfo pageInfo = this.messageQueryService.queryPageReceiveMessage(receiverUid, read, pageSize, pageNum);
			PageList pageList = PageListUtil.buildPageList(pageInfo);
			result.setPageList(pageList);
			result.setSuccess(true);
		} catch (Exception e) {
			logger.error("do messageQueryService.queryReceiveMessageByMaxId exception", e);
			result.setMsg(e.getMessage());
		}
		return result;
	}

	@Override
	public MessageResult readMessage(Long receiverUid, Long readMessageReceiveId) {
		MessageResult result = new MessageResult();
		try {
			boolean bool = this.messageQueryService.readMessage(receiverUid, readMessageReceiveId);
			result.setSuccess(bool);
		} catch (Exception e) {
			logger.error("do messageQueryService.readMessage exception", e);
			result.setMsg(e.getMessage());
		}
		return result;	
		
	}

	@Override
	public MessageResult readMessageBatch(Long receiverUid,
			List<Long> readMessageReceiveIds) {
		MessageResult result = new MessageResult();
		try {
			int count = this.messageQueryService.readMessageBatch(receiverUid, readMessageReceiveIds);
			//logger.info("batch read user message userId {} inCount {} succeseCount {}",receiverUid,readMessageReceiveIds.size(),count);
			result.setSuccess(count>0?true:false);
		} catch (Exception e) {
			logger.error("do messageQueryService.readMessage exception", e);
			result.setMsg(e.getMessage());
		}
		return result;	

	}
}
